<template>
  <router-view></router-view>
</template>
<script>
import {
  getContractDict,
  getOrg,
  getAllOrgTree,
  getBusinessType,
  getLocation,
  getZhongziApi,
  getZhongziConfig,
} from '../api/commom'
import { encryption, decrypt } from '@/utils/encryption'
import { useStore } from 'vuex'
import { servicesLoading } from '@/views/project/project-manage/loading'

export default {
  name: 'contract-manage',
  components: {},
  created() {
    const store = useStore()
    let params = {}
    console.log(this, 'this')
    if (Object.keys(store.state.projectCenter.contratDict).length == 0) {
      getContractDict().then((res) => {
        store.state.projectCenter.contratDict = res.data.data
        console.log(store.state.projectCenter.contratDict, 'store.state.projectCenter.contratDict')
      })
    }
    const getZhongziData = (paramsResult) => {
      //中咨合同语言接口
      if (!store.state.projectCenter.conLanguage.length) {
        const loading = servicesLoading({ fullscreen: true })
        getZhongziApi({
          path: '/itf/common/externalinterface/getConLanguage.json',
          cipherText: paramsResult,
        }).then((res) => {
          loading.close()
          store.state.projectCenter.conLanguage = res.data.data
        })
      }
      //中咨合同业务类型接口
      if (!store.state.projectCenter.conBusinessType.length) {
        const loading = servicesLoading({ fullscreen: true })
        getZhongziApi({
          path: '/itf/common/externalinterface/getConBusinessType.json',
          cipherText: paramsResult,
        }).then((res) => {
          loading.close()
          store.state.projectCenter.conBusinessType = res.data.data
          store.state.projectCenter.conBusinessType = arrayToTreeReduce(
            res.data.data,
            'pk',
            'parent_busitype'
          )
          console.log(store.state.projectCenter.conBusinessType, 'projectCenter.conBusinessType')
        })
      }
      //中咨业务类型接口
      if (!store.state.projectCenter.businessType.length) {
        const loading = servicesLoading({ fullscreen: true })
        getZhongziApi({
          path: '/itf/common/externalinterface/getBusitype.json',
          cipherText: paramsResult,
        }).then((res) => {
          loading.close()
          store.state.projectCenter.businessType = arrayToTreeReduce(
            res.data.data,
            'pk_busitype',
            'parent_busitype'
          )
          console.log(store.state.projectCenter.businessType, 'projectCenter.businessType')
        })
      }
      //中咨币种信息接口
      if (!store.state.projectCenter.currencies.length) {
        const loading = servicesLoading({ fullscreen: true })
        getZhongziApi({
          path: '/itf/common/externalinterface/getCurrency.json',
          cipherText: paramsResult,
        }).then((res) => {
          loading.close()
          store.state.projectCenter.currencies = res.data.data
        })
      }
      // //中咨国家与地区接口
      // if (!store.state.projectCenter.conRegion.length) {
      //   const loading = servicesLoading({ fullscreen: true })
      //   getZhongziApi({
      //     path: '/itf/common/externalinterface/getConRegion.json',
      //     cipherText: paramsResult,
      //   }).then((res) => {
      //     loading.close()
      //     store.state.projectCenter.conRegion = res.data.data
      //   })
      // }
      // 中咨境内行政区接口
      if (!store.state.projectCenter.adminRegion.length) {
        const loading = servicesLoading({ fullscreen: true })
        getZhongziApi({
          path: '/itf/common/externalinterface/getAdminRegion.json',
          cipherText: paramsResult,
        }).then((res) => {
          loading.close()
          store.state.projectCenter.adminRegion = res.data.data
          console.log(store.state.projectCenter.adminRegion, 'projectCenter.adminRegion')
        })
      }
    }
    if (!localStorage.getItem('zhongziConfig')) {
      getZhongziConfig().then((res) => {
        params = {
          appcode: res.data.data.appCode,
          username: res.data.data.username,
          password: res.data.data.password,
          pkdept: res.data.data.pkdept,
        }
        store.state.projectCenter.ZhongziConfig = params
        localStorage.setItem('zhongziConfig', JSON.stringify(params))
        let paramsResult = encryption(params)
        decrypt(paramsResult)
        getZhongziData(paramsResult)
      })
    } else {
      store.state.projectCenter.ZhongziConfig = JSON.parse(localStorage.getItem('zhongziConfig'))
      let paramsResult = encryption(store.state.projectCenter.ZhongziConfig)
      getZhongziData(paramsResult)
      console.log(store.state.projectCenter.ZhongziConfig, 'zhongziConfig')
    }
    if (store.state.projectCenter.orgList.length == 0) {
      getOrg().then((res) => {
        store.state.projectCenter.orgList = res.data.data
        ergodicOrg(store.state.projectCenter.orgList)
      })
    }
    if (store.state.projectCenter.allOrgList.length == 0) {
      getAllOrgTree().then((res) => {
        store.state.projectCenter.allOrgList = res.data.data
        ergodicOrg(store.state.projectCenter.allOrgList)
      })
    }
    if (store.state.projectCenter.incomeType.length == 0) {
      getBusinessType().then((res) => {
        store.state.projectCenter.incomeType = res.data.data
        ergodicOrg(store.state.projectCenter.incomeType)
      })
    }
    if (store.state.projectCenter.allLocationList.length == 0) {
      const loading = servicesLoading({ fullscreen: true })
      getLocation().then((res) => {
        loading.close()
        store.state.projectCenter.allLocationList = res.data.data
        console.log(store.state.projectCenter.allLocationList, 'projectCenter.allLocationList')
      }).catch(()=>{
        loading.close()
      })
    }
    const ergodicOrg = (orgList) => {
      orgList.forEach((element) => {
        element.label = element.name
        element.value = element.id
        if (element.children) {
          ergodicOrg(element.children)
        }
      })
    }
    const arrayToTreeReduce = (nodes, id, parentId) => {
      const map = {}
      const tree = nodes.reduce((acc, node) => {
        map[node[id]] = { ...node, children: [] }
        if (node[parentId] === null) {
          acc.push(map[node[id]])
        } else {
          map[node[parentId]].children.push(map[node[id]])
        }
        return acc
      }, [])
      return tree
    }
  },
}
</script>
